import http from './http/index'

export const login = ({ user, password }) => {
  // http.post
  return http.post(undefined, [{
    service: 'login.Login',
    args: {
      id: user,
      password
    }
  }], false)
}

export const logout = () => {
  return http.post(undefined, [{
    service: 'login.Logout'
  }], false)
}

export const getInfo = () => {
  return http.post(undefined, [{
    service: 'account.Info'
  }], false)
}

export const getMenus = () => {
  return http.post(undefined, [{
    service: 'account.GetMenus'
  }], false)
}

// const apiUploadAvatar = 'account.UploadAvatar'
export const uploadAvatar = new class {
  apis = ['self.UploadAvatar']
  fn = (page, imageFile) => {
    return http.upload(page, [{
      service: this.apis[0]
    }], {
      avatar: imageFile
    })
  }
}()

// const apiModifyInfo = 'account.ModifyInfo'
export const modifyInfo = new class {
  apis = ['account.EditInfo']
  fn = (page, { id, name }) => {
    return http.post(page, [{
      'service': this.apis[0],
      'args': { id, name }
    }])
  }
}()

export const modifyPassword = new class {
  apis = ['account.EditPassword']
  fn = (page, { id, oldPwd, newPwd }) => {
    // console.log('1', 'modifyPassword', { id, oldPwd, newPwd })
    return http.post(page, [{
      service: this.apis[0],
      args: { id, oldPwd, newPwd }
    }])
  }
}()

// const apiList = 'account.List'
export const list = new class {
  apis = ['account.List']
  fn(page) {
    return http.post(page, [{
      service: this.apis[0]
    }])
  }
}()

// const apiSetStatus = 'account.SetStatus'
export const setStatus = new class {
  apis = ['account.SetStatus']
  fn = (page, { id, status }) => {
    return http.post(page, [{
      service: this.apis[0],
      args: { id, status }
    }])
  }
}()

export const resetPassword = new class {
  apis = ['account.ResetPasswd']
  fn = (page, { id }) => {
    return http.post(page, [{
      service: this.apis[0],
      args: { id }
    }])
  }
}()

export const createAccount = new class {
  apis = ['account.CreateEmployee']
  fn = (page, { id, name }) => {
    return http.post(page, [{
      service: this.apis[0],
      args: { id, name }
    }])
  }
}()

// const apiListAccountsAndRoles = [
//   'account.List', 'role.List'
// ]
export const listAccountsAndRoles = new class {
  ACCOUNT_LIST = 'account.List'
  ROLE_LIST = 'account.ListRoles'
  apis = [
    this.ACCOUNT_LIST, this.ROLE_LIST
  ]
  fn = (page) => {
    return http.post(page, [{
      'service': this.ACCOUNT_LIST
    }, {
      'service': this.ROLE_LIST
    }])
  }
}()

export const setRoles = new class {
  apis = ['account.SetRoles']
  fn = (page, id, roles) => {
    return http.post(page, [{
      service: this.apis[0],
      args: {
        id: id,
        roles: roles
      }
    }])
  }
}()

// const apiSetoles = 'account.SetRoles'
// export const setRoles = {
//   apis: [apiSetoles],
//   fn(id, roles) {
//     return http.post([{
//       service: apiSetoles,
//       args: {
//         'id': id,
//         'roles': roles
//       }
//     }], true)
//   }
// }

